using System;
using System.IO;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using JMClient.Service;
using JMClient.ViewModels;
using JMClient.ViewModels.ControlsLib;
using JMClient.Views;
using JMClient.Views.ControlsLib;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Navigation.Regions;

namespace JMClient;

public partial class App : PrismApplication
{
    public override void OnFrameworkInitializationCompleted()
    {
        try
        {
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                desktop.MainWindow = Container.Resolve<MainWindow>();
            }

            base.OnFrameworkInitializationCompleted();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"App 初始化异常: {ex}");
            File.AppendAllText("crash.log", $"[App Init] {ex}\n");

            // 如果你有 SystemLog 实例，也可以写入
            var systemLog = LogManagement.GetLogger<App>(typeof(SystemLog));
            systemLog.LogError(ex, "App 初始化过程中崩溃");

            Environment.Exit(1); // 防止继续运行处于异常状态的程序
        }
    }
    private SystemLog? _systemLog { get; set; }
    protected override void OnInitialized()
    {
        base.OnInitialized();
        var rm = Container.Resolve<IRegionManager>();
        rm.RegisterViewWithRegion("NavRegion", typeof(NavSlider));
        //rm.RegisterViewWithRegion("NavContentRegion",);
        // rm.Regions["NavContentRegion"].Add(Container.Resolve<HomeView>(), "HomeView");
        // rm.Regions["NavContentRegion"].Activate(Container.Resolve<HomeView>());
    }
    protected override AvaloniaObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        
            // 设计模式下的初始化逻辑
            var systemLog = (SystemLog)LogManagement.RegisterLogInstance(typeof(SystemLog));
            // 写启动日志
            _systemLog = systemLog;
            systemLog.Write("程序启动，日志系统初始化成功", LogLevel.Information);
            containerRegistry.RegisterInstance(systemLog);
            var configService = new ConfigService(systemLog);
            containerRegistry.Register<DeviceEntityViewModel>();
            containerRegistry.RegisterInstance(configService);
            containerRegistry.RegisterForNavigation<ColorDisplayView>();
            containerRegistry.RegisterForNavigation<NavSlider>();
            containerRegistry.RegisterForNavigation<HomeView>();
            containerRegistry.RegisterForNavigation<ComponentsList,ComponentsListViewModel>();
            containerRegistry.RegisterForNavigation<DemoNotes,DemoNotesViewModel>();
            containerRegistry.RegisterDialogWindow<SandboxDialogWindow>();
            containerRegistry.RegisterDialog<SandboxWindow, SandboxWindowViewModel>("SandboxWindow");
            containerRegistry.RegisterForNavigation<SandboxWindow>();
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        
        var info = new ModuleInfo {
            ModuleName = "JMCOM",
            ModuleType = typeof(JMCOM.JMCOM).AssemblyQualifiedName,
            InitializationMode = InitializationMode.WhenAvailable
        };

        var info2 = new ModuleInfo
        {
            ModuleName = "JMLibrary",
            ModuleType = typeof(JMLibrary.JMLibrary).AssemblyQualifiedName,
            InitializationMode = InitializationMode.WhenAvailable
        };
        moduleCatalog.AddModule(info);
        moduleCatalog.AddModule(info2);
        base.ConfigureModuleCatalog(moduleCatalog);
    }
}